﻿using Microsoft.AspNetCore.Authorization;
using System.Security.Claims;

namespace _13Net8AspNetCore_AuthorizationAuthentication.Authrizes
{
    public class PasswordAuthorizationHandler : AuthorizationHandler<PassWordAuthorizationReuirement>
    {
        protected override Task HandleRequirementAsync(AuthorizationHandlerContext context, PassWordAuthorizationReuirement requirement)
        {
            // 1、获取用户信息
            Claim claim2 = context.User.Claims.FirstOrDefault(S => S.Type == "password");

            // 2、获取密码（权限）
            string userPassword = claim2.Value;

            // 3、获取接口权限
            string interfacePassword = requirement.RequiredPassword;

            // 4、鉴权
            if (userPassword == interfacePassword)
            {
                context.Succeed(requirement);
            }
            else
            {
                context.Fail(new AuthorizationFailureReason(this, "权限不足"));
            }
            return Task.CompletedTask;

        }
    }
}
